package com.stormrage.edu.serv.edu.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.stormrage.edu.dao.edu.EduProjectDao;
import com.stormrage.edu.serv.edu.EduProjectServ;
import com.stormrage.edu.serv.edu.view.EduProjectMajorServ;
import com.stormrage.entity.edu.EduProject;
import com.stormrage.entity.edu.rep.EduProjectRep;
import com.stormrage.entity.edu.req.EduProjectReq;
import com.stormrage.entity.rep.ListRep;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.FileUtil;

/**
 * @author www.stormragetech.com
 * @date 2016年1月19日
 * @version 1.0.0
 */
@Service
public class EduProjectServImpl implements EduProjectServ {

	private static final Logger logger = LoggerFactory.getLogger(EduProjectServImpl.class);

	@Autowired
	private EduProjectDao eduProjectDao;

	@Autowired
	private EduProjectMajorServ eduProjectMajorServ;

	@Override
	public Integer create(final EduProject arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 Create 】- 开始运行 ");

		Integer index = eduProjectDao.create(arg0);

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 Create 】- 完成运行 ");
		return index;
	}

	@Override
	public void update(final EduProject arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 Update 】- 开始运行 ");

		eduProjectDao.update(arg0);

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 Update 】- 完成运行 ");
	}

	@Override
	public void delete(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 Delete 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		eduProjectDao.delete(map);

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 Delete 】- 完成运行 ");
	}

	@Override
	public Integer queryCount(final EduProjectReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 QueryCount 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		Integer index = eduProjectDao.queryCount(map);

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 QueryCount 】- 完成运行 ");
		return index;
	}

	@Override
	public EduProject queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 QueryById 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		EduProject entity = eduProjectDao.queryById(map);

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 QueryById 】- 完成运行 ");
		return entity;
	}

	@Override
	public List<EduProject> queryList() throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 QueryList 】- 开始运行 ");

		List<EduProject> array = eduProjectDao.queryList();

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 QueryList 】- 完成运行 ");
		return array;
	}

	@Override
	public List<EduProjectRep> queryListForExam() throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 queryListForExam 】- 开始运行 ");

		List<EduProject> array = eduProjectDao.queryList();

		List<EduProjectRep> list = new ArrayList<EduProjectRep>();
		int seq = 1;

		Iterator<EduProject> it = array.iterator();
		while (it.hasNext()) {
			EduProject proj = it.next();
			Integer projCount = eduProjectMajorServ.queryCountById(proj.getId());
			EduProjectRep projRep = EduProjectRep.instance(proj, seq, projCount);

			logger.debug(" 生成的projRep为 " + projRep.toString());
			logger.debug(" 生成的seq为 " + seq);

			list.add(projRep);
			seq++;
		}

		logger.debug(" 生成的list为 " + list.toString());

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 queryListForExam 】- 完成运行 ");
		return list;
	}

	@Override
	public ListRep queryList(final EduProjectReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 QueryList 】- 开始运行 ");

		Map<String, Object> map = initQuery(arg0);

		Integer count = eduProjectDao.queryCount(map);
		List<EduProject> array = eduProjectDao.queryList(map);

		List<EduProjectRep> list = new ArrayList<EduProjectRep>();
		int seq = 1;

		Iterator<EduProject> it = array.iterator();
		while (it.hasNext()) {
			EduProject proj = it.next();
			Integer projCount = eduProjectMajorServ.queryCountById(proj.getId());
			EduProjectRep projRep = EduProjectRep.instance(proj, seq, projCount);
			list.add(projRep);
			seq++;
		}

		ListRep rep = ListRep.instance(count, list);

		logger.debug(" 【 Serv 】- 【 EduProjectServ 】- 【 QueryList 】- 完成运行 ");
		return rep;
	}

	@Override
	public String upload(final MultipartFile arg0, final String arg1) throws StormrageTechException {

		logger.debug(" 【 Service 】- 【 AdminItemServ 】- 【 Upload 】- 【 STARTED 】");

		String fileName = null;

		fileName = FileUtil.saveFile(arg0, arg1, "image");

		// FileUtil.saveThumb(arg1 + fileName, arg1, fileName, 360);

		logger.debug(" 【 Service 】- 【 AdminItemServ 】- 【 Upload 】- 【 COMPLETED 】");
		return fileName;
	}

	private Map<String, Object> initQuery(final EduProjectReq arg0) {

		if (null == arg0)
			return null;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startPage", arg0.getPage().getStartPage());
		map.put("size", arg0.getPage().getSize());

		StringBuffer query = new StringBuffer("");

		if (null != arg0.getProject() && arg0.getProject().length() > 0) {
			query.append("project LIKE :project AND ");
			map.put("project", "%" + arg0.getProject() + "%");
		}

		String sql = query.toString();
		if (null != sql && sql.length() > 0)
			map.put("query", " WHERE " + sql.substring(0, sql.length() - 4));
		else
			map.put("query", "");

		return map;
	}

}
